﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 管理页面控制器        ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Wedn.Net.Common;
using Wedn.Net.Model;
using Wedn.Net.UI.Controllers;
using Wedn.Net.Utility;

namespace Wedn.Net.Web.Controllers
{
    /// <summary>
    /// 管理页面控制器
    /// </summary>
    public class AdminController : AdminBaseController
    {
        private readonly IBLL.IBLLFactory _bllInstance = BLLFactory.BLLFactory.Instance;
        /// <summary>
        /// 管理页面首页框架
        /// </summary>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 管理页面首页
        /// </summary>
        public ActionResult Welcome()
        {
            return View();
        }

        #region 文章

        /// <summary>
        /// 所有文章
        /// </summary>
        public ActionResult Posts()
        {
            var terms = _bllInstance.TermManager.Select(t => true).ToList();
            IList<Term> formatCategorys = new List<Term>();
            _bllInstance.TermManager.LoadTierCategoryList(terms.Where(c => c.Type == (int)TermType.PostCategory).ToList(), formatCategorys, "─", 0);

            ViewBag.Terms = terms;
            ViewBag.FormatCategorys = formatCategorys;

            int totalCount;
            var posts = _bllInstance.PostManager.SelectPage(1, 30, out totalCount,
                p =>
                p.Type == (int)PostType.Article &&
                p.Status == (int)PostStatus.Pubilc,
                p => p.Modified, true
                ).ToList();

            var relations = _bllInstance.RelationManager.Select(r => r.ObjectType == (int)RelationObjectType.Post).ToList();
            ViewBag.Relations = relations;
            ViewBag.TotalCount = totalCount;
            ViewBag.TotalPage = Math.Ceiling(ViewBag.TotalCount / (double)30);
            return View(posts);
        }

        ///// <summary>
        ///// 标签管理
        ///// </summary>
        //public ActionResult Terms()
        //{
        //    var terms = _bllInstance.TermManager.Select(t => true);
        //    return View(terms);
        //}

        /// <summary>
        /// 所有分类
        /// </summary>
        public ActionResult Categorys()
        {
            var list = _bllInstance.TermManager.Select(c => c.Type == (int)TermType.PostCategory).ToList();
            IList<Term> categorys = new List<Term>();
            _bllInstance.TermManager.LoadTierCategoryList(list, categorys, "─", 0);
            //list.Sort();
            return View(categorys);
        }

        /// <summary>
        /// 所有标签
        /// </summary>
        public ActionResult Tags()
        {
            var tags = BLLFactory.BLLFactory.Instance.TermManager.Select(c => c.Type == (int)TermType.PostTag).ToList();
            return View(tags);
        }

        /// <summary>
        /// 撰写新文章
        /// </summary>
        public ActionResult Writer()
        {
            var terms = _bllInstance.TermManager.Select(c => c.Type == (int)TermType.PostCategory).ToList();
            IList<Term> categorys = new List<Term>();
            _bllInstance.TermManager.LoadTierCategoryList(terms, categorys, "--", 0);
            ViewBag.Categorys = categorys;
            ViewBag.PopCategorys = terms.OrderByDescending(c => c.Count).Take(10).ToList();
            ViewBag.Authors = _bllInstance.UserManager.Select(u => u.Status == (int)UserStatus.Normal && u.Role > (int)UserRole.Subscriber).ToList();
            ViewBag.CurrentUser = CurrentUser;
            return View();
        }

        /// <summary>
        /// 编辑文章
        /// </summary>
        public ActionResult Editor()
        {
            int postId = RequestHelper.QueryInt("post", -1);
            if (postId == -1)
                RedirectToAction("Writer");
            var post = _bllInstance.PostManager.SelectSingle(p => p.Id == postId);
            IList<Term> terms = _bllInstance.TermManager.Select(c => true).ToList();
            IList<Term> categorys = terms.Where(c => c.Type == (int)TermType.PostCategory).ToList();
            IList<Term> tags = terms.Where(c => c.Type == (int)TermType.PostTag).ToList();
            IList<Term> formatCategorys = new List<Term>();
            _bllInstance.TermManager.LoadTierCategoryList(categorys, formatCategorys, "--", 0);

            ViewBag.Relations = _bllInstance.RelationManager.Select(r => r.ObjectId == postId && r.ObjectType == (int)RelationObjectType.Post).ToList();
            ViewBag.Tags = tags;
            ViewBag.Categorys = formatCategorys;
            ViewBag.PopCategorys = categorys.OrderByDescending(c => c.Count).Take(10).ToList();
            ViewBag.Authors = _bllInstance.UserManager.Select(u => u.Status == (int)UserStatus.Normal && u.Role > (int)UserRole.Subscriber).ToList();
            ViewBag.CurrentPost = post;
            return View();
        }

        #endregion

        /// <summary>
        /// 媒体管理
        /// </summary>
        public ActionResult Media()
        {
            return View();
        }

        ///// <summary>
        ///// 用户登录
        ///// </summary>
        //public ActionResult Login()
        //{
        //    //User CurrentUser = Session["CurrentUser"] as User;
        //    //if (CurrentUser != null)
        //    //{
        //    //    //string redirectTo = RequestHelper.QueryString("r");
        //    //    if (string.IsNullOrEmpty(redirect))
        //    //        redirect = "/";
        //    //    return RedirectToAction("Index");
        //    //}

        //    return View();
        //}

        #region 配置

        /// <summary>
        /// 配置页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Option()
        {
            return View();
        }

        #endregion
    }
}
